package pailaidai.hc.com.mysvgdemo.view;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.StyleRes;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import pailaidai.hc.com.mysvgdemo.R;

/**
 * Created by Administrator on 2018/4/3.
 */

public class CustomDialog extends AlertDialog {
    private boolean isSingle;
    private LayoutInflater inflater;
    private TextView title, content, confirm, cancel;
    private String stitle, scontent, sconfirm, scancel;
    private View line3;
    private CustomDialogInterface customDialogInterface;

    public CustomDialogInterface getCustomDialogInterface() {
        return customDialogInterface;
    }

    public void setCustomDialogInterface(CustomDialogInterface customDialogInterface) {
        this.customDialogInterface = customDialogInterface;
    }

    public interface CustomDialogInterface {
        void confirm();
        void cancel();
    }

    public void setSingle(boolean single) {
        isSingle = single;
        if(confirm!=null&&line3!=null){
            confirm.setVisibility(isSingle ? View.VISIBLE : View.GONE);
            line3.setVisibility(isSingle ? View.VISIBLE : View.GONE);
        }
    }

    public CustomDialog(Context context, String stitle, String scontent, String sconfirm, String scancel, boolean single,CustomDialogInterface custom) {
        super(context);
        this.stitle = stitle;
        this.scontent = scontent;
        this.sconfirm = sconfirm;
        this.scancel = scancel;
        this.isSingle = single;
        this.customDialogInterface=custom;
    }

    protected CustomDialog(Context context) {
        super(context);
    }

    protected CustomDialog(Context context, @StyleRes int themeResId) {
        super(context, themeResId);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        inflater = LayoutInflater.from(getContext());
        View view = inflater.inflate(R.layout.custom_dialog, null);
        setView(view);
        super.onCreate(savedInstanceState);
        initView();
        initData();
        initListen();
    }

    private void initView() {
        title = findViewById(R.id.title);
        content = findViewById(R.id.content);
        confirm = findViewById(R.id.confirm);
        cancel = findViewById(R.id.cancel);
        line3 = findViewById(R.id.line3);
    }

    private void initData() {
        title.setText(stitle);
        content.setText(scontent);
        confirm.setText(sconfirm);
        cancel.setText(scancel);
        confirm.setVisibility(isSingle ? View.VISIBLE : View.GONE);
        line3.setVisibility(isSingle ? View.VISIBLE : View.GONE);
    }

    private void initListen() {
        confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                customDialogInterface.confirm();
                dismiss();
            }
        });
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                customDialogInterface.cancel();
                dismiss();
            }
        });
    }


}
